using System;
using System.Collections.Generic;
using System.Text;

namespace Toenda.Foundation.Utility {
	/// <summary>
	/// Class Text
	/// </summary>
	public static class Text {
		/// <summary>
		/// Find the index of a space char in a string
		/// </summary>
		/// <param name="text">The text.</param>
		/// <param name="startIndex">The start index.</param>
		/// <returns></returns>
		public static int FindSpaceCharIndex(string text, int startIndex) {
			if(text.Length > startIndex) {
				int index = ( startIndex == 0 ? -1 : 0);
				int result = 0;

				while(index < startIndex) {
					index = text.IndexOf(" ", index + 1);

					//if(index < startIndex) {
					result = index;
					//}
				}

				return result;
			}

			return -1;
		}

		/// <summary>
		/// Convert a long string into a short string
		/// </summary>
		/// <param name="text">The text.</param>
		/// <returns></returns>
		public static string ShortenString(string text) {
			return ShortenString(text, 50, StringCutPlaceholderPosition.Middle);
		}

		/// <summary>
		/// Convert a long string into a short string
		/// </summary>
		/// <param name="text">The text.</param>
		/// <param name="placeholder">The placeholder.</param>
		/// <returns></returns>
		public static string ShortenString(string text, StringCutPlaceholderPosition placeholder) {
			return ShortenString(text, 50, placeholder);
		}

		/// <summary>
		/// Convert a long string into a short string
		/// </summary>
		/// <param name="text">The text.</param>
		/// <param name="length">The length.</param>
		/// <returns></returns>
		public static string ShortenString(string text, int length) {
			return ShortenString(text, length, StringCutPlaceholderPosition.Middle);
		}

		/// <summary>
		/// Convert a long string into a short string
		/// </summary>
		/// <param name="text">The text.</param>
		/// <param name="length">The length.</param>
		/// <param name="placeholder">The placeholder.</param>
		/// <returns></returns>
		public static string ShortenString(string text, int length, StringCutPlaceholderPosition placeholder) {
			if(text.Length > length) {
				switch(placeholder) {
					case StringCutPlaceholderPosition.Nothing:
						text = text.Substring(0, length);
						break;
					
					case StringCutPlaceholderPosition.Initial:
						text = "..." 
							+ text.Substring(text.Length - length);
						break;

					case StringCutPlaceholderPosition.Middle:
						text = text.Substring(
							0, 
							( length / 2 ) - 3
						) + "..." + text.Substring(
							text.Length - ( ( length / 2 ) - 3 )
						);
						break;
						
					case StringCutPlaceholderPosition.Ending:
						text = text.Substring(0, length)
							+ "...";
						break;
				}
			}

			return text;
		}

		/// <summary>
		/// Inserts the amount of chars.
		/// </summary>
		/// <param name="targetString">The target string.</param>
		/// <param name="startIndex">The start index.</param>
		/// <param name="amount">The amount.</param>
		/// <param name="charItem">The char item.</param>
		/// <returns></returns>
		public static string InsertAmountOfChars(string targetString, int startIndex, int amount, string charItem) {
			string append = "";

			for(int i = 0; i < amount; i++) {
				append += charItem;
			}

			targetString = targetString.Insert(startIndex, append);

			return targetString;
		}

		/// <summary>
		/// Indent a text
		/// </summary>
		/// <param name="text">The text.</param>
		/// <param name="prefix">The prefix.</param>
		/// <param name="lineLength">Length of the line.</param>
		/// <returns></returns>
		public static string IndentText(string text, string prefix, int lineLength) {
			if(string.IsNullOrEmpty(text)) {
				return text;
			}

			StringBuilder result = new StringBuilder(string.Empty);
			string[] paragraphs = text.Split(Environment.NewLine.ToCharArray());

			for(int y = 0; y <= paragraphs.Length - 1; y++) {
				string p = paragraphs[y];
				if(p.Trim().Length > 0) {
					for(int i = 0; i <= p.Length; i = i + lineLength) {
						string line;

						if(p.Length >= i + lineLength) {
							line = p.Substring(i, lineLength);

							int lastSpace = line.LastIndexOf(" ");

							if(lastSpace > -1 && lastSpace < lineLength) {
								line = line.Substring(0, lastSpace);
							}

							i = i - lineLength + lastSpace;
						}
						else {
							line = p.Substring(i, p.Length - i);
						}

						result.Append(Environment.NewLine + prefix + line.TrimStart());
					}

					if(y + 1 <= paragraphs.Length - 1) {
						result.Append(Environment.NewLine + prefix);
					}
				}
			}

			text = result.ToString();

			text = text.Replace(
				Environment.NewLine + prefix + Environment.NewLine + prefix + prefix,
				Environment.NewLine + prefix + prefix
			);

			return text;
		}

		/// <summary>
		/// Returns a string with the correct and highest byte format
		/// </summary>
		/// <param name="bytes"></param>
		/// <returns></returns>
		public static string ToFuzzyByteString(long bytes) {
			double s = bytes;
			string[] format = new string[] {
				"{0} bytes", "{0} KB",  "{0} MB", "{0} GB", "{0} TB", "{0} PB", "{0} EB", "{0} ZB", "{0} YB"
			};

			int i = 0;

			while(i < format.Length && s >= 1024) {
				s = (long)( 100 * s / 1024 ) / 100.0;

				i++;
			}

			return string.Format(format[i], s);
		}
	}
}
